package com.genius.smsweb.actionmanage;

import com.genius.smsbase.Controller.GenericController;
import com.genius.smsbase.Domain.Result;
import com.genius.smsbase.util.ResultUtil;
import com.genius.smsdao.actionmanage.domain.Action;
import com.genius.smsdao.studentmanage.domain.Student;
import com.genius.smsservice.actionmanage.service.ActionManager;
import com.genius.smsservice.studentmanage.service.StudentManager;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Api(tags = "二课管理-二课课堂", description = "ActionController")
@RestController
@RequestMapping(value = "action")
public class ActionController extends GenericController<Action,Long, ActionManager> {

    ActionManager actionManager;

    @Resource
    StudentManager studentManager;

    @Autowired
    public void setActionManager(ActionManager actionManager){
        this.actionManager = actionManager;
        this.manager = this.actionManager;
    }

    /**
     * @param studentnumber,actionid
     * @return Result
     */
    @RequestMapping(value = "/join",method = RequestMethod.GET)
    public Result join(String studentnumber,String actionid){
        Student student = studentManager.findByNumber(studentnumber);
        if(Objects.isNull(student)){
            return ResultUtil.error("学生信息错误");
        }
        Action action = actionManager.findById(Long.parseLong(actionid));
        if(Objects.isNull(action)){
            return ResultUtil.error("活动信息错误");
        }
        List<Action> actionList = student.getActionList();
        if(Objects.isNull(actionList)){
            actionList = new ArrayList<Action>();
        }
        if(actionList.contains(action)){
            return ResultUtil.error("您已经参加该活动，不能重复参加");
        }
        actionList.add(action);
        student.setActionList(actionList);
        studentManager.save(student);
        return ResultUtil.success("参加成功");
    }

}
